﻿namespace SqlSugar
{
    public interface ITransactionScope
    {
        ITransactionBase BeginTran();
    }

    public sealed class TransactionScope : ITransactionScope
    {
        private readonly TransactionData _transactionData;

        public TransactionScope(ISqlSugarClient sugarClient)
        {
            _transactionData = new TransactionData
            {
                Level = 0,
                SugarClient = sugarClient
            };
        }

        public ITransactionBase BeginTran()
        {
            _transactionData.Level += 1;

            if (_transactionData.SugarClient.Ado.Transaction == null)
            {
                _transactionData.SugarClient.Ado.BeginTran();
            }

            return new TransactionBase(_transactionData);
        }
    }
}
